<?php

namespace app\api\controller;

use app\api\service\LoginService;
use app\api\service\OrderService;
use app\api\service\SampleOrderService;
use app\BaseController;
use app\Response;
use think\facade\Request;

class Order extends BaseController
{
    /** 订单列表
    */
    public function orderList(){
        if (Request::isPost()) {
            $user_id = LoginService::getUserIdFromToken();
            if (!is_numeric($user_id)) {
                return $user_id;
            }
            $page = Request::post("page",1);
            $num = Request::post("num",10);
            $state = Request::post("state","");
            $info = OrderService::orderListService($user_id,$page,$num,$state);
            return $info;
        }
    }


    public function robOrder(){
        if (Request::isPost()) {
            $user_id = LoginService::getUserIdFromToken();
            if (!is_numeric($user_id)) {
                return $user_id;
            }

        }
    }

    /***   样品订单   ***/
    public function sampleOrderList(){
        if (Request::isPost()) {
            $user_id = LoginService::getUserIdFromToken();
            if (!is_numeric($user_id)) {
                return $user_id;
            }
            $page = Request::post("page",1);
            $num = Request::post("num",10);
            $state = Request::post("state","");
            $info = SampleOrderService::sampleOrderListService($user_id,$page,$num,$state);
            return $info;
        }
    }

    public function sampleOrderInfo(){
        if (Request::isPost()) {
            $user_id = LoginService::getUserIdFromToken();
            if (!is_numeric($user_id)) {
                return $user_id;
            }
            $id = Request::post("id");
            if(empty($id)){
                return Response::wrapError(0,"参数异常!");
            }
            $info = SampleOrderService::sampleOrderInfoService($id,$user_id);
            return $info;
        }
    }

    public function sampleOrderWxPay(){
        if (Request::isPost()) {
            $user_id = LoginService::getUserIdFromToken();
            if (!is_numeric($user_id)) {
                return $user_id;
            }
            $id = Request::post("id");
            if(empty($id)){
                return Response::wrapError(0,"参数异常!");
            }
            $info = SampleOrderService::sampleOrderWxPayService($id,$user_id);
            return $info;
        }
    }


    public function sampleOrderBalancePay(){
        if (Request::isPost()) {
            $user_id = LoginService::getUserIdFromToken();
            if (!is_numeric($user_id)) {
                return $user_id;
            }
            $id = Request::post("id");
            $pay_pw = Request::post("pay_pw");
            if(empty($id)){
                return Response::wrapError(0,"参数异常!");
            }
            if(empty($pay_pw)){
                return Response::wrapError(0,"请输入支付密码!");
            }
            $info = SampleOrderService::sampleOrderBalancePay($id,$user_id,$pay_pw);
            return $info;
        }
    }

    /** 下样品订单
    */
    public function robSampleOrder(){
        if (Request::isPost()) {
            $user_id = LoginService::getUserIdFromToken();
            if (!is_numeric($user_id)) {
                return $user_id;
            }
            $data = $this->request->post();
            try{
                $this->validate($data, 'RobSampleOrder');
            }catch (\Exception $e){
                return Response::wrapError(0,$e->getMessage());
            }
            $res = SampleOrderService::robSampleOrderService($data,$user_id);
            return $res;
        }
    }

    public function sampleOrderEdit(){
        if (Request::isPost()) {
            $user_id = LoginService::getUserIdFromToken();
            if (!is_numeric($user_id)) {
                return $user_id;
            }
            $id = Request::post("id");
            $state = Request::param("state","");
            if(empty($id)){
                return Response::wrapError("订单信息获取异常!");
            }
            $res = SampleOrderService::robSampleOrderEditService($user_id,$id,$state);
            return $res;
        }
    }


}